package com.exmaple.baidumv.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.baidumv.R;
import com.exmaple.baidumv.entity.RankingEntity;
import com.yuelan.codelib.utils.ImageDownloader;
import com.yuelan.codelib.utils.ImageDownloader.OnImageDownload;
import com.yuelan.codelib.utils.UIHelper;

public class RankingAdapter extends BaseAdapter {

	private Context mContext ;
	private LayoutInflater mInflater ;
	private ArrayList<RankingEntity> arrayList ;
 	public RankingAdapter (Context context){
		mContext = context;
		mInflater = LayoutInflater.from(context);
	}
 	
 	public void setList(ArrayList<RankingEntity> list){
 		arrayList = list;
 		this.notifyDataSetChanged();
 	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(arrayList != null){
			return arrayList.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final ViewHolder viewHodler;
		if(arg1 == null){
			viewHodler = new ViewHolder();
			arg1 = mInflater.inflate(R.layout.item_list_ranking , null);
			viewHodler.mMoviePicture =(ImageView) arg1.findViewById(R.id.movie_picture);
			viewHodler.mMovieName = (TextView) arg1.findViewById(R.id.movie_name);
			viewHodler.mMovieType = (TextView) arg1.findViewById(R.id.movie_type);
			viewHodler.mMovieTime = (TextView) arg1.findViewById(R.id.movie_time);
			viewHodler.mMovieIntroduction = (TextView) arg1.findViewById(R.id.movie_introduction);
			viewHodler.mClickPlay = (TextView) arg1.findViewById(R.id.click_play);
			arg1.setTag(viewHodler);
		}else{
			viewHodler = (ViewHolder)arg1.getTag();
		}
		viewHodler.mMovieName.setText(arrayList.get(arg0).getName());
		viewHodler.mMovieType.setText("类型＝" + arrayList.get(arg0).getType());
		viewHodler.mMovieTime.setText("时间＝" + arrayList.get(arg0).getTimes());
		viewHodler.mMovieIntroduction.setText("介绍＝" + arrayList.get(arg0).getMs());
		ImageDownloader.imageDownload(arrayList.get(arg0).getImgUrl(),mContext,new OnImageDownload() {

			@Override
			public void onDownloadSucc(Bitmap bitmap) {
				if (bitmap != null) {
					viewHodler.mMoviePicture.setImageBitmap(bitmap);
				}
			}
		}, 800);
		
		viewHodler.mClickPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIHelper.showWebView(mContext , arrayList.get(arg0).getMvUrl() , arrayList.get(arg0).getName());
            }
        });
		return arg1;
	}
	class ViewHolder{
		private ImageView mMoviePicture;
		private TextView mMovieName;
		private TextView mMovieType;
		private TextView mMovieTime;
		private TextView mMovieIntroduction;
		private TextView mClickPlay;
	}

}
